//
//  DatePickerView.m
//  APP_login
//
//  Created by edz on 7/7/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "DatePickerView.h"
#import <Masonry.h>

@interface DatePickerView()

@property(nonatomic) UIView *pickView;
@property(nonatomic) UIDatePicker *birthDatePicker;
@property(nonatomic) UIButton *pickViewCancelButton;
@property(nonatomic) UIButton *pickViewConfromButton;

@end



@implementation DatePickerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor =  [UIColor colorWithWhite:0.1 alpha:0.3];
        
        self.pickView = [[UIView alloc] init];
        [self addSubview: _pickView];
        [_pickView mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(0);
            make.bottom.mas_equalTo(400);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(400);
        }];
        _pickView.backgroundColor = [UIColor whiteColor];
        
        self.pickViewCancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_pickView addSubview: _pickViewCancelButton];
        [_pickViewCancelButton mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(30);
        }];
        _pickViewCancelButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_pickViewCancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_pickViewCancelButton addTarget:self action:@selector(pickViewCancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        self.pickViewConfromButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_pickView addSubview: _pickViewConfromButton];
        [_pickViewConfromButton mas_makeConstraints:^(MASConstraintMaker *make){
            make.right.mas_equalTo(0);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(30);
        }];
        _pickViewConfromButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_pickViewConfromButton setTitle:@"确认" forState:UIControlStateNormal];
        [_pickViewConfromButton addTarget:self action:@selector(pickViewConfromButtonClick) forControlEvents:UIControlEventTouchUpInside];
         
        self.birthDatePicker = [[UIDatePicker alloc] init];
        [_pickView addSubview:_birthDatePicker];
        _birthDatePicker.datePickerMode = UIDatePickerModeDate;
        [_birthDatePicker mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(50);
            make.left.right.bottom.mas_equalTo(0);
        }];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self removeView];
}

- (void) pickViewCancelButtonClick{
   [self removeView];
}

- (void) pickViewConfromButtonClick{
    NSDate *chosesDate = _birthDatePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY年MM月dd日";
    NSString *str = [dateFormatter stringFromDate:chosesDate];
    //判断代理是否为空，是否是代理函数的代理与属性的代理相同
    if (self.delegate && [self.delegate respondsToSelector:@selector(datePickerViewSetBirthdayData:)]) {
        [self.delegate datePickerViewSetBirthdayData:str];
    }
    [self removeView];
}

- (void) showView:(UIView*)view{
    [view addSubview:self];
    [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.pickView mas_updateConstraints:^(MASConstraintMaker *make){
            make.bottom.mas_equalTo(0);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finish){}];
}

- (void) removeView{
    [UIView animateWithDuration:0.3 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
       [self.pickView mas_updateConstraints:^(MASConstraintMaker *make){
           make.bottom.mas_equalTo(400);
       }];
       [self layoutIfNeeded];
    } completion:^(BOOL finish){
        [self removeFromSuperview];
    }];
}

@end
